<?php
/**
 * 车展基金主线模型
 * @author qianxun
 *
 */
class carProcessModel extends MY_Model {
    function carProcessModel() {
        parent::MY_Model();
        $this->table = 'CarProcess';
    }
    /**
     * 包装数据
     * {@inheritDoc}
     * @see MY_Model::packing()
     */
    function packing($list = array()){
        //时间单位配置
        $this->CI->load->model('timeUnitModel');
        $timeUnitList = $this->CI->timeUnitModel->getCacheData('timeUnitList');
        //载入项目模型
        $this->CI->load->model('carProjectModel');
        foreach ($list as $k => $v){
            //时间单位
            $list[$k]['timeUnitStr'] = isset($timeUnitList[$v['year']][$v['timeUnit']]) ? $timeUnitList[$v['year']][$v['timeUnit']] : '';
            //预算审核状态
            $projectData = $this->CI->carProjectModel->getNewData(array('procId'=>$v['id']));
            $list[$k]['budgetAuditStr'] = $projectData ? $projectData['budgetAuditStr'] : '未申报';
            //结算审核状态
            $list[$k]['settleAuditStr'] = $projectData ? $projectData['settleAuditStr'] : '未申报';
        }
        return $list;
    }
    /**
     * 包装后查询列表,并以时间单位为键
     * @param mix $where
     * @return Ambigous
     */
    function getNewListByTimeUnit($where, $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = $this->getList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $k => $v){
            $list[(int)$v['timeUnit']] = $v;
        }
        return $this->packing($list);
    }
    /**
     * 获取主流程列表，并以专营店ID为主键
     * @param array $where
     * @param string $limit
     * @param string $offset
     * @param string $colum
     * @param string $orderby
     * @return array
     */
    function getNewListByStoreId($where, $limit = NULL, $offset = NULL, $colum = '', $orderby = ''){
        $findList = $this->getList($where, $limit, $offset, $colum, $orderby);
        $list = array();
        foreach ($findList as $k => $v){
            $list[(int)$v['storeId']] = $v;
        }
        return $list;
    }
    /**
     * 初始化主流程数据
     * @param array $data
     */
    function initData($data = array()){
        if (!isset($data['storeId']) || !$data['storeId']){
            showError('请设置店铺ID');
        }
        if (!isset($data['year']) || !$data['year']){
            showError('请设置年份');
        }
        if (!isset($data['timeUnit']) || !$data['timeUnit']){
            showError('请设置时间');
        }
        $procId = $this->upset($data, array('createTime'=>time()));
        if ($procId){
            return $procId;
        }else{
            showError('生成失败');
        }
    }
    /**
     * 统计金额明细
     */
    function countMoneyDetail($procId = 0){
        $this->CI->load->model('carProjectModel');
        $projectMoney = $this->CI->carProjectModel->getMoney($procId);
        $data = array(
            'siteNum' => $projectMoney['siteNum'],//站点数
            'budgetApplyGx' => $projectMoney['budgetApplyGx'],//广宣预算申请费用
            'budgetApplyRent' => $projectMoney['budgetApplyRent'],//场租预算申请费用
            'budgetApplyMatching' => $projectMoney['budgetApplyMatching'],//车展配套预算费用
            'budgetApply' => $projectMoney['budgetApply'],//预算申请
            'budgetAudit' => $projectMoney['budgetAudit'],//预算审核
            //'subsidyGx' => $projectMoney['subsidyGx'],//广宣活动补贴
            'settleApplyGx' => $projectMoney['settleApplyGx'],//广宣结算申请费用
            'settleApplyRent' => $projectMoney['settleApplyRent'],//场租结算申请费用
            //'subsidyMatching' => $projectMoney['subsidyMatching'],//配套活动补贴
            'settleApplyMatching' => $projectMoney['settleApplyMatching'],//车展配套结算申请费用
            'settleStoreTotal' => $projectMoney['settleStoreTotal'],//参展专营店实际出资总额
            'settleQiTotal' => $projectMoney['settleQiTotal'],//启辰店实际出资总额
            'settleRmpSupport' => $projectMoney['settleRmpSupport'],//RMP支持场租费用
            'settleApply' => $projectMoney['settleApply'],//结算申请
            'settleAudit' => $projectMoney['settleAudit'],//结算审核
        );
        return $data;
    }
    /**
     * 更新金额
     * @param int $procId 主线流程ID
     */
    function updateMoney($procId){
        $findData = $this->getData($procId);
        $moneyDetail = $this->countMoneyDetail($procId);
        $data = array(
            'siteNum' => $moneyDetail['siteNum'],//站点数
            'budgetApplyGx' => $moneyDetail['budgetApplyGx'],
            'budgetApplyRent' => $moneyDetail['budgetApplyRent'],
            'budgetApplyMatching' => $moneyDetail['budgetApplyMatching'],
            'budgetApply' => $moneyDetail['budgetApply'],
            'budgetAudit' => $moneyDetail['budgetAudit'],
            'subsidyGx' => $moneyDetail['subsidyGx'],//广宣活动补贴
            'settleApplyGx' => $moneyDetail['settleApplyGx'],
            'settleApplyRent' => $moneyDetail['settleApplyRent'],//租结算申请金额
            'subsidyMatching' => $moneyDetail['subsidyMatching'],//车展配套补贴额度
            'settleApplyMatching' => $moneyDetail['settleApplyMatching'],
            'settleStoreTotal' => $moneyDetail['settleStoreTotal'],//参展专营店实际出资总额
            'settleQiTotal' => $moneyDetail['settleQiTotal'],//启辰店实际出资总额
            'settleRmpSupport' => $moneyDetail['settleRmpSupport'],//RMP支持场租费用
            'settleApply' => $moneyDetail['settleApply'],
            'settleAudit' => $moneyDetail['settleAudit'],
        );
        $this->CI->load->model('importMoneyModel');
        //广宣活动补贴
        $data['subsidyGx'] = $this->CI->importMoneyModel->getMoney($findData['storeId'], $findData['timeUnit'], 31, $findData['year']);
        //车展配套补贴额度
        $data['subsidyMatching'] = $this->CI->importMoneyModel->getMoney($findData['storeId'], $findData['timeUnit'], 33, $findData['year']);
        //广宣可结算金额
        $data['canSettleGx'] = min($data['subsidyGx'], $data['settleApplyGx']);
        //配套可结算金额
        $data['canSettleMatching'] = min($data['subsidyMatching'], $data['settleApplyMatching']);
        //本站结算金额
        $actualSettleMoney = $this->CI->importMoneyModel->getMoney($findData['storeId'], $findData['timeUnit'], 34, $findData['year']);
        $data['settleMoney'] = $actualSettleMoney ? $actualSettleMoney : ($data['canSettleGx'] + $data['settleApplyRent'] + $data['canSettleMatching']);
        //KPI系数
        $data['kpi'] = $this->CI->importMoneyModel->getMoney($findData['storeId'], $findData['timeUnit'], 35, $findData['year']);
        //最终结算金额
        $data['finalSettleMoney'] = ($data['canSettleGx'] + $data['canSettleMatching']) * $data['kpi'] + $data['settleApplyRent'];
        //待同步状态
        $data['isSync'] = 0;
        //更新
        $this->edit($procId, $data);
    }
    /**
     * 统计各结算状态数
     * @param int $id 单店ID
     * @param number $isBubao
     * @return multitype:number unknown
     */
    function getSettleStatusCount($procId, $isBubao=0){
        $count = array(
            -1  => 0,//未提交
            0   => 0,//未审核
            1   => 0,//已通过
            2   => 0,//不通过
            3   => 0,//返回修改
            4   => 0,//部分通过
        );
        //单店基金-项目活动
        $this->CI->load->model('carProjectModel');
        $projectCount = $this->CI->carProjectModel->getSettleStatusCount($procId, $isBubao);
    
        $count[-1] = $projectCount[-1];
        $count[0] = $projectCount[0];
        $count[1] = $projectCount[1];
        $count[2] = $projectCount[2];
        $count[3] = $projectCount[3];
        $count[4] = $projectCount[4];
        return $count;
    }
    /**
     * 分析列表，统计出条件专营店ID集的总和
     * @param array $sellpointIds 专营店id集
     * @param array $list 以专营店id为键的列表
     * @return array
     */
    function countTotalListByStoreIds($storeIds = array(),$list = array()){
        $data = array(
            'storeCount' => 0,//申请店数
            'fixedMoney' => 0,//固定额度（双月额度）
            'siteNum' => 0,//站点数
            'budgetApply'=>0,//预算申请
            'settleApply'=>0,//结算申请
            'settleAudit'=>0,//结算审核
            'settleMoney' => 0,//结算金额
            'finalSettleMoney' => 0,//最终结算金额
        );
        $filterStoreId = array_intersect($storeIds, array_keys($list));
        foreach ($filterStoreId as $storeId){
            if(isset($list[$storeId])){
                $data['storeCount'] ++;//申请店数累加
                $data['budgetApply']    += $list[$storeId]['budgetApply'];//预算申请
                $data['budgetAudit']    += $list[$storeId]['budgetAudit'];//预算审核
                $data['settleApply']    += $list[$storeId]['settleApply'];//结算申请
                $data['settleAudit']    += $list[$storeId]['settleAudit'];//结算审核
                $data['settleMoney']    += $list[$storeId]['settleMoney'];//本站结算金额
                $data['finalSettleMoney'] += $list[$storeId]['finalSettleMoney'];//最终结算金额
            }
        }
        return $data;
    }
}